package clientssl

import (
	"crypto/tls"
	"encoding/json"
	"errors"
	"go-web/base"
	"io/ioutil"
	"net/http"
	"strings"

)

func BcjClient(url string, content []string) ([]bool, error) {
	htr := &http.Transport{
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true,
		},
	}
	transData := strings.Join(content, "\n")
	client := &http.Client{Transport: htr}
	res, err := client.Post(url, "text/plain", strings.NewReader(transData))
	if err != nil {
		return nil, err
	}

	defer res.Body.Close()
	if res.StatusCode != 200 {
		return nil, errors.New(base.Err_Invalid)
	}

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}
	switch string(body) {
	case base.Err_ReadDataFail:
		return nil, errors.New(base.Err_ReadDataFail)
	case base.Err_MarshalFail:
		return nil, errors.New(base.Err_MarshalFail)
	}
	var result []bool
	err = json.Unmarshal(body, &result)
	if err != nil {
		return nil, err
	}
	return result, err
}
